<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>FusionCharts Documentation</title>
<link rel="stylesheet" href="Style.css" type="text/css" />
</head>

<body>
<table width="98%" border="0" cellspacing="0" cellpadding="3" align="center">
  <tr> 
    <td><h2 class="pageHeader">Using FusionCharts with RoR - Charting Data from an Array </h2></td>
  </tr>
  <tr>
   <td valign="top" class="text"><p>In this section, we'll show you how to use FusionCharts and Ruby to plot charts from data contained in Ruby arrays. We'll cover the following examples here:</p>
      <ul>
	  <li>Creating a single series chart from data contained in arrays </li>
      <li>Creating a multi series chart from data contained in arrays</li>
      </ul>
      <p><strong>Before you go further with this page, we recommend you to please see the previous section &quot;Basic Examples&quot; as we start off from concepts explained in that page. </strong></p>
      </td>
  </tr>
      <tr>
    <td valign="top" class="highlightBlock">All code discussed here is present in <br><span class="codeInline">Controller : Download Package > Code > RoR > app > controllers > array_example_controller.rb</span>. <br> <span class="codeInline">Rhtml : Download Package > Code > RoR > app > views > array_example</span> folder. </td>
  </tr>
  <tr>
    <td valign="top" class="text">&nbsp;</td>
  </tr>
   <tr>
    <td valign="top" class="header">Creating a single-series chart from data contained in arrays </td>
  </tr>
  <tr>
    <td valign="top" class="text">Let us now create a single-series chart from data contained in arrays. 
  </tr>
  <tr>
    <td valign="top" class="codeBlock">
    <p><b>Controller:array_example_controller.rb <br>  
      Action: singleseries</b><br>
       class ArrayExampleController < ApplicationController<br>
&nbsp;def singleseries<br>
&nbsp;&nbsp;<span class="codeComment">&nbsp;#Let's store the sales data for 6 products in our array. We also store the name of products. <br>
&nbsp;&nbsp;&nbsp;#Store Name of Products , sales data</span><br>
&nbsp;&nbsp;&nbsp;arrData=[<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ["Product A",567500],<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ["Product B",815300],<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ["Product C",556800],<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ["Product D",734500],<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ["Product E",676800],<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ["Product F",648500]<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]<br>
&nbsp;&nbsp;<span class="codeComment">&nbsp;#Now, we need to convert this data into XML. We convert using string concatenation.</span><br>
&nbsp;&nbsp;&nbsp;strXML=''<br>
&nbsp;<span class="codeComment">&nbsp;&nbsp;#Initialize &lt;chart&gt; element</span><br>
&nbsp;&nbsp;&nbsp;strXML = "&lt;chart caption='Sales by Product' numberPrefix='$' formatNumberScale='0'&gt;"<br>
&nbsp;&nbsp;<span class="codeComment">&nbsp;#Convert data to XML and append</span><br>
&nbsp;&nbsp;&nbsp;index=0<br>
&nbsp;&nbsp;&nbsp;until index > arrData.length - 1<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strXML = strXML + "&lt;set label='" + arrData[index][0].to_s + "' value='" + arrData[index][1].to_s + "' /&gt;"<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;index+=1<br>
&nbsp;&nbsp;&nbsp;end<br>
&nbsp;<span class="codeComment">&nbsp;&nbsp;#Close &lt;chart&gt; element</span><br>
&nbsp;&nbsp;&nbsp;strXML = strXML + "&lt;/chart&gt;"<br>
&nbsp;&nbsp;&nbsp;<span class="codeComment">#Create the chart - Column 3D Chart with data contained in strXML</span><br>
&nbsp;&nbsp;&nbsp;<strong>@chart=renderChart("/FusionCharts/Column3D.swf", "", strXML, "productSales", 600, 300, false, false)</strong><br>
&nbsp;end<br/>
      end</p>
    <p><font color="blue"><br/>
          </font>
        <b>View:</b><br>
      
        <font color="green"> &lt;HTML&gt;<br />
&nbsp;&nbsp;&nbsp;&lt;HEAD&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;TITLE&gt;	FusionCharts - Array Example using Single Series Column 3D Chart	&lt;/TITLE&gt;
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="codeComment">&nbsp;</span><br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;SCRIPT LANGUAGE="Javascript" SRC="/FusionCharts/FusionCharts.js"&gt;;&lt;/SCRIPT&gt;<br />
&nbsp;&nbsp;&nbsp;&lt;/HEAD&gt;<br />
&nbsp;&nbsp;&nbsp;&lt;BODY&gt;<br/>
  <strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;%= @chart%&gt;</strong><br/>
&nbsp;&nbsp;&nbsp;&lt;/BODY&gt;<br/>
&lt;/HTML&gt;</p>
    </p></td>
    </p></td>
  </tr><tr>
    <td valign="top" class="text"><p>In the above example, we first include <span class="codeInline">FusionCharts.js</span> file to enable us embed the chart using JavaScript. 
    <p>Thereafter, we define an Ruby array <span class="codeInline">arrData</span> to store sales data for 6 different products. The array has two columns - first one for data label and the next one for data values. </p>
    <p>We define a variable <span class="codeInline">strXML</span> to store the entire XML data. To build the XML, we iterate through the array and using string concatenation. Finally, we render the chart using <span class="codeInline">renderChart()</span> function and pass <span class="codeInline">strXML</span> as <span class="codeInline">dataXML</span>. </p>
    <p>When you view the chart, you'll see a chart as under: </p></td>
  </tr>
 
   <tr>
    <td valign="top" class="text"><img src="Images/Code_ArraySS.jpg" alt="" width="584" height="287" /></td>
  </tr>
   <tr>
    <td valign="top" class="header">Creating a multi-series chart from data contained in arrays</td>
  </tr>
  <tr>
    <td valign="top" class="text">The code to create a multi series chart can be listed as under: </td>
  </tr>

  <tr>
    <td valign="top" class="codeBlock">
    <p><b>Controller: array_example_controller.rb <br>  
      Action: multiseries</b><br>
      class ArrayExampleController < ApplicationController<br>
  &nbsp;def multiseries<br>
  &nbsp;&nbsp;&nbsp;<span class="codeComment">#Let's store the sales data for 6 products in our array. We also store<br>
&nbsp;&nbsp;&nbsp;#the name of products. <br>
&nbsp;&nbsp;&nbsp;#Store Name of Products</span><br>
  &nbsp;&nbsp;&nbsp;arrData =[<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;["Product A",567500,547300],<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;["Product B",815300,584500],<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;["Product C",556800,754000],<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;["Product D",734500,456300],<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;["Product E",676800,754500],<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;["Product F",648500,437600],<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]<br>
  &nbsp;<span class="codeComment">&nbsp;&nbsp;#Now, we need to convert this data into multi-series XML. <br>
&nbsp;&nbsp;&nbsp;#We convert using string concatenation.<br>
&nbsp;&nbsp;&nbsp;#strXML - Stores the entire XML<br>
&nbsp;&nbsp;&nbsp;#strCategories - Stores XML for the &lt;categories&gt; and child &lt;category&gt; elements<br>
&nbsp;&nbsp;&nbsp;#strDataCurr - Stores XML for current year's sales<br>
&nbsp;&nbsp;&nbsp;#strDataPrev - Stores XML for previous year's sales</span><br>
  &nbsp;&nbsp;&nbsp;strXML=''<br>
  &nbsp;&nbsp;&nbsp;strCategories=''<br>
  &nbsp;&nbsp;&nbsp;strDataCurr=''<br>
  &nbsp;&nbsp;&nbsp;strDataPrev=''<br>
  &nbsp;&nbsp;<span class="codeComment">&nbsp;#Initialize &lt;chart&gt; element</span><br>
  &nbsp;&nbsp;&nbsp;strXML = "&lt;chart caption='Sales by Product' numberPrefix='$' formatNumberScale='1' rotateValues='1' placeValuesInside='1' <br>
  &nbsp;&nbsp;&nbsp;decimals='0' &gt;"<br>
  &nbsp;&nbsp;&nbsp;<span class="codeComment">#Initialize &lt;categories&gt; element - necessary to generate a multi-series chart</span><br>
  &nbsp;&nbsp;&nbsp;strCategories = "&lt;categories&gt;"<br>
  &nbsp;&nbsp;&nbsp;<span class="codeComment">#Initiate &lt;dataset&gt; elements</span><br>
  &nbsp;&nbsp;&nbsp;strDataCurr = "&lt;dataset seriesName='Current Year'&gt;"<br>
  &nbsp;&nbsp;&nbsp;strDataPrev = "&lt;dataset seriesName='Previous Year'&gt;"<br>
  &nbsp;&nbsp;&nbsp;index=0<br>
  &nbsp;&nbsp;&nbsp;<span class="codeComment">#Iterate through the data </span><br>
  &nbsp;&nbsp;&nbsp;until index >  arrData.length - 1<br>
  &nbsp;&nbsp;&nbsp;<span class="codeComment">&nbsp;&nbsp;#Append &lt;category name='...' /&gt; to strCategories</span><br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strCategories = strCategories + "&lt;category name='" + &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arrData[index][0].to_s + "' /&gt;"<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="codeComment">#Add &lt;set value='...' /&gt; to both the datasets</span><br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strDataCurr = strDataCurr + "&lt;set value='" + arrData[index][1].to_s + "' /&gt;"<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strDataPrev = strDataPrev + "&lt;set value='" + arrData[index][2].to_s + "' /&gt;"<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;index+=1<br>
  &nbsp;&nbsp;&nbsp;end<br>
  &nbsp;&nbsp;<span class="codeComment">&nbsp;#Close &lt;categories&gt; element</span><br>
  &nbsp;&nbsp;&nbsp;strCategories = strCategories + "&lt;/categories&gt;"<br>
  &nbsp;&nbsp;&nbsp;<span class="codeComment">#Close &lt;dataset&gt; elements</span><br>
  &nbsp;&nbsp;&nbsp;strDataCurr = strDataCurr + "&lt;/dataset&gt;"<br>
  &nbsp;&nbsp;&nbsp;strDataPrev = strDataPrev + "&lt;/dataset&gt;"<br>
  &nbsp;<span class="codeComment">&nbsp;&nbsp;#Assemble the entire XML now</span><br>
  &nbsp;&nbsp;&nbsp;strXML = strXML + strCategories + strDataCurr + strDataPrev + "&lt;/chart&gt;"<br>
  &nbsp;&nbsp;<span class="codeComment">&nbsp;# headers['Content-Type'] = "text/xml"<br>
&nbsp;&nbsp;&nbsp;#render :text=> strXML,:layout=>false<br>
&nbsp;&nbsp;&nbsp;#Create the chart - MS Column 3D Chart with data contained in strXML</span><br>
      <strong>&nbsp;&nbsp;&nbsp;@chart=renderChart("/FusionCharts/MSColumn3D.swf", "", strXML, "productSales", 600, 300, false, false)<br>
        </strong>&nbsp;end<br>   
      end</p>
    <p><font color="blue"><br>
        </font>
      
      <b>View:</b><br>
      
      
      <font color="green"> &lt;HTML&gt;<br />
&nbsp;&nbsp;&nbsp;&lt;HEAD&gt;
      <br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;SCRIPT LANGUAGE="Javascript" SRC="/FusionCharts/FusionCharts.js"&gt;&quot;&gt;&lt;/SCRIPT&gt;<br />
&nbsp;&nbsp;&nbsp;&lt;/HEAD&gt;<br />
&nbsp;&nbsp;&nbsp;&lt;BODY&gt;<br/>
  <strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;%= @chart%&gt;</strong>
  <br />
&nbsp;&nbsp;&nbsp;&lt;/BODY&gt;<br/>
&lt;/HTML&gt;</p>
    </p></td>
    </p></td>
  </tr>
    <tr>
    <td valign="top" class="text"><p>In the above example, we first include <span class="codeInline">FusionCharts.js</span> file to enable us embed the chart using JavaScript. 
      <p>Thereafter, we define an Ruby array <span class="codeInline">arrData</span> to store sales data for 6 different products. The array has three columns - first one for data label (product) and the next two for data values. The first data value column would store sales information<br />
      for current year and the second one for previous year.</p>
      <p>We define a variable <span class="codeInline">strXML</span> to store the entire XML data. We also define <span class="codeInline">strCategories</span>, <span class="codeInline">strDataCurr</span> and <span class="codeInline">strDataPrev</span> variables to store XML data for categories elements, current year's dataset and previous year's dataset respectively. To build the XML, we iterate through the array and using string concatenation. We concatenate the entire XML finally in <span class="codeInline">strXML</span>. </p>
      <p>Finally, we render the chart using <span class="codeInline">renderChart()</span> function and pass <span class="codeInline">strXML</span> as <span class="codeInline">dataXML</span>. </p>
    <p>When you view the chart, you'll see a chart as under: </p></td>
  </tr>
   <tr>
    <td valign="top" class="text"><img src="Images/Code_ArrayMS.jpg" width="584" height="287"></td>
  </tr>
 <tr>
    <td valign="top" class="highlightBlock">In <span class="codeInline">Download Package > Code > RoR > app > controllers > array_example_controller.rb</span>, we've more example codes to create Stacked and Combination Charts too, which have not been explained here, as they're similar in concept. You can directly see the code if you want to. </td>
  </tr>
</table>
</body>
</html>
